﻿@using Microsoft.AspNetCore.Components.Forms
@using System.IO
@using System.Linq
@using Covenant.Models.Listeners

@using BlazorInputFile

<EditForm Model="Listener" OnValidSubmit="OnFormSubmit">
    <DataAnnotationsValidator />
    <div class="form-group">
        <label for="Description">Description</label>
        <input id="Description" name="Description" @bind="@Listener.Description" class="form-control-plaintext" readonly>
    </div>
    <div class="form-group">
        <label for="Name">Name</label>
        <input id="Name" name="Name" @bind="@Listener.Name" disabled="@Disabled" class="form-control">
        <div class="text-danger"><ValidationMessage For="() => Listener.Name" /></div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-4">
            <label for="BindAddress">BindAddress</label>
            <input id="BindAddress" name="BindAddress" @bind="@Listener.BindAddress" disabled="@Disabled" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => Listener.BindAddress" /></div>
        </div>
        <div class="form-group col-md-4">
            <label for="BindPort">BindPort</label>
            <input id="BindPort" name="BindPort" @bind="@Listener.BindPort" disabled="@Disabled" class="form-control" type="number">
            <div class="text-danger"><ValidationMessage For="() => Listener.BindPort" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-4">
            <label for="ConnectPort">ConnectPort</label>
            <input id="ConnectPort" name="ConnectPort" @bind-value="Listener.ConnectPort" @bind-value:event="oninput" disabled="@Disabled" class="form-control" type="number">
            <div class="text-danger"><ValidationMessage For="() => Listener.ConnectPort" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-4 mb-0">
            <label for="ConnectAddresses_0_">ConnectAddresses</label>
        </div>
        <div class="form-group col-md-4 mb-0">
            <label for="Urls_0_">Urls</label>
        </div>
    </div>
    @for (int i = 0; i < Listener.ConnectAddresses.Count; i++)
    {
        int number = i;
        <div class="form-row">
            <div class="form-group col-md-4">
                <input id=@("ConnectAddresses_" + number + "_") name=@("ConnectAddresses[" + number + "]") @bind-value="Listener.ConnectAddresses[number]" @bind-value:event="oninput" disabled="@Disabled" class="form-control">
            </div>
            <div class="form-group col-md-4">
                <input id="Urls_i_" name="Urls[i]" value="@Listener.Urls[number]" class="form-control" readonly>
            </div>
            @if (number != 0)
            {
                <div class="col-auto align-self-center">
                    <button type="button" @onclick="(e => RemoveRow(number))" disabled="@Disabled" class="btn btn-outline-danger btn-sm">
                        <span class="fe fe-x"></span>
                    </button>
                </div>
            }
        </div>
    }
    <div class="text-danger"><ValidationMessage For="() => Listener.ConnectAddresses" /></div>
    <div class="text-danger"><ValidationMessage For="() => Listener.Urls" /></div>
    <button type="button" @onclick="AddRow" disabled="@Disabled" class="btn btn-outline-primary btn-sm mt-2 mb-4">
        <span class="fe fe-plus"></span> Add
    </button>

    <div class="form-row">
        <div class="form-group col-md-3">
            <label for="UseSSL">UseSSL</label>
            <select id="UseSSL" name="UseSSL" @onchange="(e => UpdateUseSSL(e))" disabled="@Disabled" class="form-control form-control-sm">
                <option value="False">False</option>
                <option value="True">True</option>
            </select>
            <div class="text-danger"><ValidationMessage For="() => Listener.UseSSL" /></div>
        </div>

        <div class="form-group col-md-3 cert-div" hidden="@(!Listener.UseSSL)">
            <label for="SSLCertificateFile">SSLCertificate</label>
            <InputFile id="SSLCertificateFile" name="SSLCertificateFile" class="form-control-file"
                        disabled="@Disabled"
                        OnChange="FileUpload" />
        </div>

        <div class="form-group col-md-6 cert-div" hidden="@(!Listener.UseSSL)">
            <label for="SSLCertificatePassword">SSLCertificatePassword</label>
            <input id="SSLCertificatePassword" name="SSLCertificatePassword"
                    @bind="Listener.SSLCertificatePassword" disabled="@Disabled" class="form-control form-control-sm">
            <div class="text-danger"><ValidationMessage For="() => Listener.SSLCertificatePassword" /></div>
        </div>
    </div>
    <div class="form-group">
        <label for="ProfileId">HttpProfile</label>
        <select id="ProfileId" name="ProfileId" @bind="Listener.ProfileId" class="form-control">
            @foreach (var Profile in Profiles)
            {
                <option value="@Profile.Id">@Profile.Name</option>
            }
        </select>
        <div class="text-danger"><ValidationMessage For="() => Listener.ProfileId" /></div>
    </div>
    @if (!Disabled)
    {
        <button type="submit" class="btn btn-primary">
            <span class="fe fe-@SubmitIcon"></span> @SubmitLabel
        </button>
    }
</EditForm>

@code {
    [Parameter]
    public HttpListener Listener { get; set; }

    [Parameter]
    public IEnumerable<Profile> Profiles { get; set; }

    [Parameter]
    public string SubmitIcon { get; set; } = "edit";

    [Parameter]
    public string SubmitLabel { get; set; } = "Edit";

    [Parameter]
    public EventCallback<HttpListener> OnSubmit { get; set; }

    [Parameter]
    public bool Disabled { get; set; } = false;

    private void UpdateRow(int row, ChangeEventArgs e)
    {
        Listener.ConnectAddresses[row] = e.Value.ToString();
    }

    private void AddRow()
    {
        Listener.ConnectAddresses.Add("");
    }

    private void RemoveRow(int row)
    {
        Listener.ConnectAddresses.RemoveAt(row);
    }

    private void UpdateUseSSL(ChangeEventArgs e)
    {
        if (bool.TryParse(e.Value.ToString(), out bool result))
        {
            Listener.UseSSL = result;
        }
    }

    private async Task FileUpload(IFileListEntry[] files)
    {
        IFileListEntry file = files.FirstOrDefault();
        if (file != null)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                await file.Data.CopyToAsync(ms);
                Listener.SSLCertificate = Convert.ToBase64String(ms.ToArray());
            }
        }
    }

    private async Task OnFormSubmit()
    {
        await this.OnSubmit.InvokeAsync(this.Listener);
    }
}
